<?php

class UserpageController extends Controller
{

	public function filters()
	{
		return array(
              'accessControl',
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',
		'actions'=>array('index','cvv','MyCv','Addcv','EditCv','CV','Editinformation','Myinformation','UserInfo','MyVacancy','AddVacancy','Vacancy','Editvacancy'),
		'roles'=>array('admin'),
		),
			array('allow',
                'actions'=>array('index','cvv','MyCv','Addcv','EditCv','CV','Editinformation','Myinformation','UserInfo','MyVacancy','AddVacancy','Vacancy','Editvacancy'),
                'users'=>array('*'),
		),
		
		
			array('deny',
                'actions'=>array('index','cvv'),
                'users'=>array('?'),
		),
		
		array('deny',
                'actions'=>array('MyCv','Addcv','EditCv','CV'),
                'roles'=>array('employer'),
		),
	
		);
	}

	public function actionIndex()
	{

		$usInfo = Profile::model()->findAll('user_id=:user_id',array('user_id'=>Yii::app()->user->id));


		if ($usInfo==null)
		 {
			$profile = new Profile();
			$profile->user_id=Yii::app()->user->id;
			$profile->save();
			}
			
		$role = Yii::app()->user->getRole();
		if ($role=='student')
		{
			$this->redirect('/userpage/mycv');
		}
		elseif ($role=='employer') {
			$this->redirect('/userpage/myvacancy');
				
		}
		elseif ($role=="admin")
		{
			$this->redirect('/userpage/myvacancy');
		}
	//	$this->redirect('/admin/qmain');
	}


	public function actionEditinformation()
	{

		if (isset($_POST['name'])) 
			{
				
					$usInfoo = Profile::model()->findByPk($_POST['id']);	
								
					//$usInfoo->attributes=$_POST['Profile'];
					$usInfoo->name=$_POST['name'];
					$usInfoo->last_name=$_POST['last_name'];
					$usInfoo->second_name=$_POST['second_name'];
					$usInfoo->adress=$_POST['adress'];
									
					$usInfoo->save();				
					
			}
			$profile= new Profile();
		$pr=Profile::model()->findAll('user_id=:user_id',array(':user_id'=>Yii::app()->user->id));
		$this->render('editInformation',array('profile'=>$profile,'pr'=>$pr));
		
	}
	
	public function actionMyinformation() 
	{
		$pr=Profile::model()->findAll('user_id=:user_id',array(':user_id'=>Yii::app()->user->id));
		$this->render('myinformtion',array('pr'=>$pr));
	}
	
	
	
	public function  actionMyCv() 
	{
		$usercv=Usercv::model()->findAll('user_id=:user_id',array(':user_id'=>Yii::app()->user->id));
		$this->render('cvlist',array('usercv'=>$usercv));
	}
	
	public function actionAddcv() 
	{
		$usercv = new Usercv();
		
		  if (isset($_POST['ajax'])&& $_POST['ajax']==='reg-form')
	            {
	                # code...
	                echo CActiveForm::validate($reguser);
	                Yii::app()->end();
	            }
		
		if (isset($_POST['Usercv']))
		{			
			 $usercv->attributes=$_POST['Usercv'];
			//print_r($_POST['Usercv']) ;
			
			if ($usercv->validate('reg'))
			{
				 
				 $usercv->user_id = Yii::app()->user->id;
				 $usercv->scope_id=$_POST['scope'];
				 $usercv->date=date();

				 if($usercv->save())
				 {
				 	$this->redirect('/userpage/Mycv/');
				 }
			}
		}
		//$usercv = new Usercv();
		$scope = Scope::model()->findAll();

		// при помощи listData создаем массив вида $ключ=>$значение
        $list = CHtml::listData($scope, 'scope_id', 'scope');
		$this->render('addcv',array('usercv'=>$usercv,'list'=>$list,'scope'=>$scope));
	}
	
	
	public function actionEditCv()
	{
		$id=$_GET['id'];
		
		
	 if (isset($_POST['ajax'])&& $_POST['ajax']==='reg-form')
	            {
	                # code...
	                echo CActiveForm::validate($reguser);
	                Yii::app()->end();
	            }
		
		if (isset($_POST['Usercv']))
		{			
			$cv=Usercv::model()->findAll("user_id=:user_id AND id=:id",array(":user_id"=>Yii::app()->user->id,":id"=>$id));
			 $cv->attributes=$_POST['Usercv'];
			//print_r($_POST['Usercv']) ;
			
			if ($cv->validate('reg'))
			{
				 
				// $usercv->user_id = Yii::app()->user->id;
				 $cv->scope_id=$_POST['scope'];
				 //$usercv->date=date();

				 if($cv->save())
				 {
				 	$this->redirect('/userpage/Mycv/');
				 }
			}
		}
		$cv=Usercv::model()->findAll('user_id=:user_id AND id=:id',array(':user_id'=>Yii::app()->user->id,'id'=>$id));
		$usercv = new Usercv();
		$scope = Scope::model()->findAll();

		// при помощи listData создаем массив вида $ключ=>$значение
        $list = CHtml::listData($scope, 'scope_id', 'scope');
		$this->render('editcv',array('usercv'=>$usercv,'list'=>$list,'scope'=>$scope,'cv'=>$cv));
	}
	
	
	public function actionCV()
	{
		$id= $_GET['id'];
		$usercv= Usercv::model()->with('User','Scope')->findByPk($id);
		$this->render('cv',array('usercv'=>$usercv));
	}
	
	
	//Все что ниже касается вакансий
	
	public function actionUserInfo() 
	{
		$id= $_GET['id'];
		$user = User::model()->findByPk($id);
		if ($user->role=="employer") 
		{
			$uservacancy= Job::model()->findAll("user_id=:user_id",array(":user_id"=>$user->user_id));
			$this->render('userInfoEmployer',array('uservacancy'=>$uservacancy,'user'=>$user));
		}
		elseif($user->role=="student")
		{
			$usercv= Usercv::model()->findAll("user_id=:user_id",array(":user_id"=>$user->user_id));
			$this->render('userInfostudent',array('usercv'=>$usercv,'user'=>$user));
		}
	}
	
	
	public function actionMyVacancy()
	{
		$userVacancy=Job::model()->findAll('user_id=:user_id',array(':user_id'=>Yii::app()->user->id));
		$this->render('vacancylist',array('uservacancy'=>$userVacancy));
	}
	
	
	public function actionAddVacancy()
	{
		$uservacancy = new Job();
		
		  if (isset($_POST['ajax'])&& $_POST['ajax']==='reg-form')
	            {
	                # code...
	                echo CActiveForm::validate($reguser);
	                Yii::app()->end();
	            }
		
		if (isset($_POST['Job']))
		{			
			 $uservacancy->attributes=$_POST['Job'];
			//print_r($_POST['Usercv']) ;
			
			if ($uservacancy->validate('reg'))
			{
				 
				 $uservacancy->user_id = Yii::app()->user->id;
				 $uservacancy->scope_id=$_POST['scope'];
				 $uservacancy->date = date('Y-m-d H:i:s');
				 $uservacancy->block=0;
				 $uservacancy->complaint=0;
				
				 if($uservacancy->save())
				 {
				 	$this->redirect('/userpage/Myvacancy/');
				 }
			}
		}
		//$usercv = new Usercv();
		$scope = Scope::model()->findAll();

		// при помощи listData создаем массив вида $ключ=>$значение
        $list = CHtml::listData($scope, 'scope_id', 'scope');
		$this->render('addvacancy',array('uservacancy'=>$uservacancy,'list'=>$list,'scope'=>$scope));
	}
	
	public function actionVacancy()
	{
		$id= $_GET['id'];
		$uservacancy = Job::model()->with('User','Scope')->findByPk($id);
		$this->render('vacancy',array('uservacancy'=>$uservacancy));
	}
	
	public function actionEditvacancy()
	{
		$id=$_GET['id'];
		
		$vacancy=Job::model()->findAll('user_id=:user_id AND job_id=:job_id',array(':user_id'=>Yii::app()->user->id,'job_id'=>$id));
	 if (isset($_POST['ajax'])&& $_POST['ajax']==='reg-form')
	            {
	                # code...
	                echo CActiveForm::validate($reguser);
	                Yii::app()->end();
	            }
		
		if (isset($_POST['Job']))
		{		
			
						
			$vacancy[0]->attributes=$_POST['Job'];
		
			
			if ($vacancy[0]->validate('reg'))
			{
				 
				// $usercv->user_id = Yii::app()->user->id;
				 $vacancy[0]->scope_id=$_POST['scope'];
				 //$usercv->date=date();

				 if($vacancy[0]->save())
				 {
				 	$this->redirect('/userpage/Myvacancy/');
				 }
			}
		}
		
		$uservacancy = new Job();
		$scope = Scope::model()->findAll();

		// при помощи listData создаем массив вида $ключ=>$значение
        $list = CHtml::listData($scope, 'scope_id', 'scope');
		$this->render('editvacancy',array('uservacancy'=>$uservacancy,'list'=>$list,'scope'=>$scope,'vacancy'=>$vacancy));
	}
	
	//
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
	// return the filter configuration for this controller, e.g.:
	return array(
	'inlineFilterName',
	array(
	'class'=>'path.to.FilterClass',
	'propertyName'=>'propertyValue',
	),
	);
	}

	public function actions()
	{
	// return external action classes, e.g.:
	return array(
	'action1'=>'path.to.ActionClass',
	'action2'=>array(
	'class'=>'path.to.AnotherActionClass',
	'propertyName'=>'propertyValue',
	),
	);
	}
	*/
}